﻿// <copyright file="DataRowExtensions.cs" company="Edge Extensions Project">
// Copyright (c) 2009 All Rights Reserved
// </copyright>
// <author>Kevin Nessland</author>
// <email>kevinnessland@gmail.com</email>
// <date>2009-07-08</date>
// <summary>Contains data-related extension methods.</summary>
namespace Edge.Extensions.Data
{
    using System;
    using System.Data;

    /// <summary>
    /// Provides extension methods to the <see cref="DataRow">DataRow</see> object.
    /// </summary>
    public static class DataRowExtensions
    {
        /// <summary>
        /// Returns the <see cref="Boolean">Boolean</see> value of the specified column.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <returns>The <see cref="Boolean">Boolean</see> value stored in the specified column.</returns>
        /// <remarks>If the specified column contains a null, the return value will be <c>False</c>.</remarks>
        public static bool GetBooleanValue(this DataRow input, string columnName)
        {
            return GetBooleanValue(input, columnName, false);
        }

        /// <summary>
        /// Returns the <see cref="Boolean">Boolean</see> value of the specified column.
        /// If the value is null, the supplied value is used.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <param name="nullValue">The <see cref="Boolean">Boolean</see> value to return if the desired column contains a null value.</param>
        /// <returns>The <see cref="Boolean">Boolean</see> value stored in the specified column.</returns>
        public static bool GetBooleanValue(this DataRow input, string columnName, bool nullValue)
        {
            if (input.IsNull(columnName) == false && input[columnName].GetType().ToString() == typeof(bool).ToString())
            {
                return Convert.ToBoolean(input[columnName]);
            }
            else
            {
                return nullValue;
            }
        }

        /// <summary>
        /// Returns the <see cref="Byte">Byte</see> value of the specified column.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <returns>The <see cref="Byte">Byte</see> value stored in the specified column.</returns>
        public static byte GetByteValue(this DataRow input, string columnName)
        {
            return GetByteValue(input, columnName, 0);
        }

        /// <summary>
        /// Returns the <see cref="Byte">Byte</see> value of the specified column.
        /// If the value is null, the supplied value is used.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <param name="nullValue">The <see cref="Byte">Byte</see> value to return if the desired column contains a null value.</param>
        /// <returns>The <see cref="Byte">Byte</see> value stored in the specified column.</returns>
        public static byte GetByteValue(this DataRow input, string columnName, byte nullValue)
        {
            if (input.IsNull(columnName) == false && input[columnName].GetType().ToString() == typeof(byte).ToString())
            {
                return Convert.ToByte(input[columnName]);
            }
            else
            {
                return nullValue;
            }
        }

        /// <summary>
        /// Returns the <see cref="DateTime">DateTime</see> value of the specified column.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <returns>The <see cref="DateTime">DateTime</see> value stored in the specified column.</returns>
        /// <remarks>If the specified column contains a null, the return value will be a Null value.</remarks>
        public static DateTime GetDateTimeValue(this DataRow input, string columnName)
        {
            return GetDateTimeValue(input, columnName, DateTime.MinValue);
        }

        /// <summary>
        /// Returns the <see cref="DateTime">DateTime</see> value of the specified column.
        /// If the value is null, the supplied value is used.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <param name="nullValue">The <see cref="DateTime">DateTime</see> value to return if the desired column contains a null value.</param>
        /// <returns>The <see cref="DateTime">DateTime</see> value stored in the specified column.</returns>
        public static DateTime GetDateTimeValue(this DataRow input, string columnName, DateTime nullValue)
        {
            if (input.IsNull(columnName) == false && input[columnName].GetType().ToString() == typeof(DateTime).ToString())
            {
                return Convert.ToDateTime(input[columnName]);
            }
            else
            {
                return nullValue;
            }
        }

        /// <summary>
        /// Returns the <see cref="Decimal">Decimal</see> value of the specified column.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <returns>The <see cref="Decimal">Decimal</see> value stored in the specified column.</returns>
        /// <remarks>If the specified column contains a null, the return value will be 0.</remarks>
        public static decimal GetDecimalValue(this DataRow input, string columnName)
        {
            return GetDecimalValue(input, columnName, 0);
        }

        /// <summary>
        /// Returns the <see cref="Decimal">Decimal</see> value of the specified column.
        /// If the value is null, the supplied value is used.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <param name="nullValue">The <see cref="Decimal">Decimal</see> value to return if the desired column contains a null value.</param>
        /// <returns>The <see cref="Decimal">Decimal</see> value stored in the specified column.</returns>
        public static decimal GetDecimalValue(this DataRow input, string columnName, decimal nullValue)
        {
            if (input.IsNull(columnName) == false && input[columnName].GetType().ToString() == typeof(decimal).ToString())
            {
                return Convert.ToDecimal(input[columnName]);
            }
            else
            {
                return nullValue;
            }
        }

        /// <summary>
        /// Returns the <see cref="Integer">Integer</see> value of the specified column.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <returns>The <see cref="Integer">Integer</see> value stored in the specified column.</returns>
        /// <remarks>If the specified column contains a null, the return value will be 0.</remarks>
        public static int GetInt32Value(this DataRow input, string columnName)
        {
            return GetInt32Value(input, columnName, 0);
        }

        /// <summary>
        /// Returns the <see cref="Integer">Integer</see> value of the specified column.
        /// If the value is null, the supplied value is used.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <param name="nullValue">The <see cref="Integer">Integer</see> value to return if the desired column contains a null value.</param>
        /// <returns>The <see cref="Integer">Integer</see> value stored in the specified column.</returns>
        public static int GetInt32Value(this DataRow input, string columnName, int nullValue)
        {
            if (input.IsNull(columnName) == false && input[columnName].GetType().ToString() == typeof(int).ToString())
            {
                return Convert.ToInt32(input[columnName]);
            }
            else
            {
                return nullValue;
            }
        }

        /// <summary>
        /// Returns the <see cref="Long">Long</see> value of the specified column.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <returns>The <see cref="Long">Long</see> value stored in the specified column.</returns>
        /// <remarks>If the specified column contains a null, the return value will be 0.</remarks>
        public static long GetInt64Value(this DataRow input, string columnName)
        {
            return GetInt64Value(input, columnName, 0);
        }

        /// <summary>
        /// Returns the <see cref="Long">Long</see> value of the specified column.
        /// If the value is null, the supplied value is used.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <param name="nullValue">The <see cref="Long">Long</see> value to return if the desired column contains a null value.</param>
        /// <returns>The <see cref="Long">Long</see> value stored in the specified column.</returns>
        public static long GetInt64Value(this DataRow input, string columnName, long nullValue)
        {
            if (input.IsNull(columnName) == false && input[columnName].GetType().ToString() == typeof(long).ToString())
            {
                return Convert.ToInt64(input[columnName]);
            }
            else
            {
                return nullValue;
            }
        }

        /// <summary>
        /// Returns the <see cref="Short">Short</see> value of the specified column.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <returns>The <see cref="Short">Short</see> value stored in the specified column.</returns>
        /// <remarks>If the specified column contains a null, the return value will be 0.</remarks>
        public static short GetInt16Value(this DataRow input, string columnName)
        {
            return GetInt16Value(input, columnName, 0);
        }

        /// <summary>
        /// Returns the <see cref="Short">Short</see> value of the specified column.
        /// If the value is null, the supplied value is used.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <param name="nullValue">The <see cref="Short">Short</see> value to return if the desired column contains a null value.</param>
        /// <returns>The <see cref="Short">Short</see> value stored in the specified column.</returns>
        public static short GetInt16Value(this DataRow input, string columnName, short nullValue)
        {
            if (input.IsNull(columnName) == false && input[columnName].GetType().ToString() == typeof(short).ToString())
            {
                return Convert.ToInt16(input[columnName]);
            }
            else
            {
                return nullValue;
            }
        }

        /// <summary>
        /// Returns the <see cref="String">String</see> value of the specified column.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <returns>The <see cref="String">String</see> value stored in the specified column.</returns>
        /// <remarks>If the specified column contains a null, the return value will be a blank string ("").</remarks>
        public static string GetStringValue(this DataRow input, string columnName)
        {
            return GetStringValue(input, columnName, string.Empty);
        }

        /// <summary>
        /// Returns the <see cref="String">String</see> value of the specified column.
        /// If the value is null, the supplied value is used.
        /// </summary>
        /// <param name="input">The <see cref="DataRow">DataRow</see> containing the desired data.</param>
        /// <param name="columnName">Name of the column to retrieve the desired data from.</param>
        /// <param name="nullValue">The <see cref="String">String</see> value to return if the desired column contains a null value.</param>
        /// <returns>The <see cref="String">String</see> value stored in the specified column specifying that the value should be equal to "USAAC" if the database value is Null.</returns>
        public static string GetStringValue(this DataRow input, string columnName, string nullValue)
        {
            if (input.IsNull(columnName) == false && input[columnName].GetType().ToString() == typeof(string).ToString())
            {
                return Convert.ToString(input[columnName]);
            }
            else
            {
                return nullValue;
            }
        }
    }
}
